package com.example.blog.web;

import com.example.blog.annotation.LoginUser;
import com.example.blog.annotation.RequiresPermission;
import com.example.blog.dao.LikePostOrCommentRepository;
import com.example.blog.dao.PostsInfoRepository;
import com.example.blog.entity.LikePostOrComment;
import com.example.blog.entity.PostsInfo;
import com.example.blog.exception.NewWifiLoginException;
import com.example.blog.util.Result;
import com.example.blog.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.blog.constant.ApiUrlConstants.ADMIN;
import static com.example.blog.constant.ApiUrlConstants.USER;

/**
 * @ClassName: LikeController
 * @Description:

 * @Date 2024/1/27
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/v1")
@Api(value = "帖子点赞",description = "帖子点赞")
public class LikeController {
    @Resource
    LikePostOrCommentRepository likePostOrCommentRepository;
    @Resource
    PostsInfoRepository postsInfoRepository;
    @ApiOperation(value = "帖子-取消点赞")
    @DeleteMapping("like/{id}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result delLike(@LoginUser String userId, @PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        likePostOrCommentRepository.deleteById(id);
        return Result.ok();
    }
    @ApiOperation(value = "帖子-点赞")
    @PostMapping("/Like/{postId}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result addLikePost(@LoginUser String userId,@PathVariable String postId,@RequestBody LikePostOrComment likePostOrComment){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        likePostOrComment.setUserId(userId);
        likePostOrComment.setContentId(postId);
        likePostOrComment.setStatus(1);
        likePostOrCommentRepository.save(likePostOrComment);
        Optional<PostsInfo> byId = postsInfoRepository.findById(postId);
        byId.get().setLikeCount(byId.get().getLikeCount()+1);
        postsInfoRepository.save(byId.get());
        return Result.ok();
    }
    @ApiOperation(value = "帖子-获取是否点赞")
    @GetMapping("/like/{id}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result getLike(@LoginUser String userId,@PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        List<LikePostOrComment> allById = likePostOrCommentRepository.findAllById(Collections.singleton(id));
        List<LikePostOrComment> collect = allById.stream().filter(likePostOrComment -> likePostOrComment.getUserId().equals(userId)).collect(Collectors.toList());
        if (collect.size() > 0) {
            return Result.ok().setData(true);
        }
        return Result.ok().setData(false);
    }
}
